<?php

namespace App\Admin\Controllers\Weikong;

use App\Models\Device;
use App\Models\Project;
use App\Models\ProjectPlan;
use App\Models\DeviceType;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Log;
use SuperEggs\DcatDistpicker\Form\Distpicker;
use Dcat\Admin\Widgets\Form as Form1;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Tab;
use App\Models\ProjectUser;
use Dcat\Admin\Form\NestedForm;
// use App\Admin\Extensions\Form\ApiSelect\ApiSelect;

class CreatePointController extends Controller
{
    public function index(){
        return redirect('admin/device/guizi/'.session('guizi'));
    }

    public function create(Content $content){
        return $content
            ->title('新增采集点')
            ->description('创建')
            ->body($this->form());
    }
    public function form(){
         return Form::make(new Device(), function (Form $form) {
            $form->action('PostPoint');
            // $form->text('cid','设备ID');
            // $form->text('devicename','设备名称');
            // $form->text('gatewayId','平台编号');
            // $form->hidden('device_type')->value('8');
            // $form->text('Min','最小预警值');
            // $form->text('Max','最大预警值');
            $res = Device::where(['pid'=>session('guizi'),'device_type'=>10])->pluck('group','deviceId');
            $form->select('pid','所在分组')->options($res)->required();

            $form->table('table', function (NestedForm $table) {
                $table->text('cid','设备ID');
                $table->text('devicename','设备名称');
                $table->text('gatewayId','电信平台设备编号')->required();
                $table->text('Min','最小预警值')->required();
                $table->text('Max','最大预警值')->required();
                $table->hidden('device_type')->value('8');
            });
            $form->saving(function (Form $form) {
                if ($form->isCreating()) {
                    $table = $form->table;
                    $pid = $form->pid;
                    $form->deleteInput('table');
                    $form->deleteInput('pid');
                    if (isset($table)) {
                        foreach ($table as $key => &$value) {
                            unset($value['_remove_']);
                            $value['pid'] = $pid;
                            Device::insert($value);
                        }
                    }
                }
                // return $form->redirect('device/guizi/'.session('guizi'), '添加成功');
            });
            $form->saved(function (Form $form, $result) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    // 自增ID
                    $newId = $result;
                    // 也可以这样获取自增ID
                    // $newId = $form->getKey();
                    if (! $newId) {
                        return $form->error('数据保存失败');
                    }else{
                        Device::destroy(['deviceId'=>$newId]);
                    }
                    return;
                }
                // 修改操作
            });

        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('采集点编辑')
            ->description('编辑')
            ->body($this->form1($id)->edit($id));
    }

    public function form1(){
         return Form::make(new Device(), function (Form $form) {
            // $str = request()->fullUrl();
            // print_r(request()->fullUrl());die;
            // $form->action('PointEditPost/125');
            // $form->action(request()->fullUrl());
            $form->text('cid','设备ID');
            $form->text('gatewayId','平台编号');
            $form->text('devicename','设备名称');
            $form->text('gatewayId','电信平台设备编号')->required();
            $form->text('Min','最小预警值')->required();
            $form->text('Max','最大预警值')->required();
            // $form->saved(function (Form $form) {
            //    return $form->redirect('device/guizi/'.session('guizi'),'修改成功');
            //     // return $form->redirect('device/guizi/'.session('guizi'), '添加成功');
            // });
        });
    }


    public function update($id,Request $request){
        // print_r($request->all());die;
        // return
        // return redirect('device/guizi/'.session('guizi'));
        return $this->form1()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function destroy($id){
        return $this->form()->destroy($id);
    }
}

